﻿@using System.Globalization
@using NetSurveillance.WebApp.DataServices.Models
@model IEnumerable<AlertSetting>
@{
    ViewBag.Title = Resources.MenuResource.SystemSetting_AlertSetting;
    Layout = "~/Views/Shared/_SystemConfigLayout.cshtml";
    ViewBag.CurrentMiddleMenu = 4;
    ViewBag.CurrentMainMenu = 5;
    AlertSetting smsAlertSetting = null, emailAlertSetting = null;
    if (Model != null)
    {
        smsAlertSetting = Model.FirstOrDefault(p => p.AlertType == 1);
        emailAlertSetting = Model.FirstOrDefault(p => p.AlertType == 2);
    }
}

<h3 id="contentHeader">
    @ViewBag.Title
</h3>
@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    <div id="monitorContent" class="config_content0">
        <fieldset style="display: none;">
            <legend>短信报警设置</legend>
            <table>
                <tr>
                    <td>开启报警功能</td>
                    <td>
                        @Html.Hidden("hid_sms_id", smsAlertSetting == null ? 0 : smsAlertSetting.Id)
                        <label>
                            @Html.RadioButton("rd_sms_isOpen", 1, 
                                              smsAlertSetting != null && smsAlertSetting.StatusToken > 0,
                                              new{id="rd_sms_isOpen_yes"})
                            是
                        </label>
                        <label>
                            @Html.RadioButton("rd_sms_isOpen", 0, 
                                              smsAlertSetting != null && smsAlertSetting.StatusToken <= 0,
                                              new{id="rd_sms_isOpen_no"})
                            否
                        </label>
                    </td>
                </tr>
                <tr>
                    <td>报警时间间隔</td>
                    <td>
                        @Html.TextBox("txt_sms_alertTimespan",
                                      smsAlertSetting == null ? "" : smsAlertSetting.AlertTimespan.ToString(CultureInfo.InvariantCulture))
                        分钟
                    </td>
                </tr>
                <tr>
                    <td>设备类型</td>
                    <td>
                        @Html.TextBox("txt_sms_deviceType",
                                      smsAlertSetting == null ? "" : smsAlertSetting.DeviceType)
                    </td>
                </tr>
                <tr>
                    <td>设备波特率</td>
                    <td>
                        @Html.TextBox("txt_sms_baudRate",
                                      smsAlertSetting == null ? "" : smsAlertSetting.BaudRate.ToString(CultureInfo.InvariantCulture))
                    </td>
                </tr>
                <tr>
                    <td>短信设备串口</td>
                    <td>
                        @Html.TextBox("txt_sms_serialPort",
                                      smsAlertSetting == null ? "" : smsAlertSetting.SerialPort)
                    </td>
                </tr>
                <tr>
                    <td>报警通知号码</td>
                    <td>
                        @Html.TextBox("txt_sms_alertPhone", smsAlertSetting == null ? "" : smsAlertSetting.AlertPhone)
                    </td>
                </tr>
            </table>
        </fieldset>
        <fieldset>
            <legend>邮件报警设置</legend>
            <table>
                <tr>
                    <td>开启报警功能</td>
                    <td>
                        @Html.Hidden("hid_email_id", emailAlertSetting == null ? 0 : emailAlertSetting.Id)
                        <label>
                            @Html.RadioButton("rd_email_isOpen", 1, 
                                              emailAlertSetting != null && emailAlertSetting.StatusToken > 0,
                                              new { id = "rd_email_isOpen_yes" })
                            是
                        </label>
                        <label>
                            @Html.RadioButton("rd_email_isOpen", 0, 
                                              emailAlertSetting != null && emailAlertSetting.StatusToken <= 0,
                                              new { id = "rd_email_isOpen_no" })
                            否
                        </label>
                    </td>
                </tr>
                <tr>
                    <td>报警时间间隔</td>
                    <td>
                        @Html.TextBox("txt_email_alertTimespan",
                                      emailAlertSetting == null ? "" : emailAlertSetting.AlertTimespan.ToString(CultureInfo.InvariantCulture))
                        分钟
                    </td>
                </tr>
                <tr>
                    <td>发送者邮箱</td>
                    <td>
                        @Html.TextBox("txt_email_senderEmail",
                                      emailAlertSetting == null ? "" : emailAlertSetting.SenderEmail)
                    </td>
                </tr>
                <tr>
                    <td>Smtp服务器</td>
                    <td>
                        @Html.TextBox("txt_email_smtpServer", emailAlertSetting == null ? "" : emailAlertSetting.SmtpServer)
                    </td>
                </tr>
                <tr>
                    <td>Smtp帐号</td>
                    <td>
                        @Html.TextBox("txt_email_smtpAccount", emailAlertSetting == null ? "" : emailAlertSetting.SmtpAccount)
                    </td>
                </tr>
                <tr>
                    <td>Smtp密码</td>
                    <td>
                        @Html.TextBox("txt_email_smtpPass", emailAlertSetting == null ? "" : emailAlertSetting.SmtpPassword)
                    </td>
                </tr>
                <tr>
                    <td>报警通知邮箱地址</td>
                    <td>
                        @Html.TextBox("txt_email_alertEmail", emailAlertSetting == null ? "" : emailAlertSetting.AlertEmail)
                    </td>
                </tr>
            </table>
        </fieldset>
        <div class="buttonsContain">
            @Html.Hidden("alertSettings")
            <ul class="config_bottom_btndivs">
                <li>
                    <input type="submit" id="btnSaveQuery" value="@Resources.Resource.Button_Apply"/>   
                </li>
                <li>
                    <input type="reset" id="btnResetChanges" value="@Resources.Resource.Button_ResetChanges"/>
                </li>
            </ul>
        </div>
    </div>
}
@section css{
    <style type="text/css">
        #contentHeader
        {
            text-align: center;
            margin: 5px 0 10px 0;
            font-size: 1.5em;
        }
    </style>
}
@section javascript
{
    <script type="text/javascript">
        $(function () {
            $('#btnSaveQuery').click(function () {
                var alertSettings = [];
                alertSettings.push(getSmsEntry());
                alertSettings.push(getEmailEntry());
                $('#alertSettings').val(JSON.stringify(alertSettings));
                $('form')[0].submit();
                return true;
            });
            $('#rd_sms_isOpen_yes,#rd_sms_isOpen_no,#rd_email_isOpen_yes,#rd_email_isOpen_no').click(function () {
                var $rd = $(this), isYes = ($rd.attr('id') == 'rd_sms_isOpen_yes' || $rd.attr('id') == 'rd_email_isOpen_yes'),
                    $eles = $rd.closest('tr').siblings().find(':input');
                if (isYes)
                    $eles.removeAttr('disabled');
                else
                    $eles.attr('disabled', true);
            });
            checkIsNoOpen();
        });
        
        function getSmsEntry() {
            var smsEntry = {};
            smsEntry.StatusToken = $('#rd_sms_isOpen_yes').is(':checked') ? 1 : 0;
            smsEntry.AlertTimespan = $('#txt_sms_alertTimespan').val().trim();
            smsEntry.AlertTimespan = smsEntry.AlertTimespan == '' ? 0 : smsEntry.AlertTimespan;
            smsEntry.DeviceType = $('#txt_sms_deviceType').val().trim();
            smsEntry.BaudRate = $('#txt_sms_baudRate').val().trim();
            smsEntry.BaudRate = smsEntry.BaudRate == '' ? 0 : smsEntry.BaudRate;
            smsEntry.SerialPort = $("#txt_sms_serialPort").val().trim();
            smsEntry.AlertPhone = $('#txt_sms_alertPhone').val().trim();
            smsEntry.Id = $('#hid_sms_id').val();
            smsEntry.AlertType = 1;
            return smsEntry;
        }

        function getEmailEntry() {
            var emailEntry = {};
            emailEntry.StatusToken = $('#rd_email_isOpen_yes').is(':checked') ? 1 : 0;
            emailEntry.AlertTimespan = $('#txt_email_alertTimespan').val().trim();
            emailEntry.AlertTimespan = emailEntry.AlertTimespan == '' ? 0 : emailEntry.AlertTimespan;
            emailEntry.SmtpServer = $('#txt_email_smtpServer').val().trim();
            emailEntry.SmtpAccount = $('#txt_email_smtpAccount').val().trim();
            emailEntry.SmtpPassword = $('#txt_email_smtpPass').val().trim();
            emailEntry.AlertEmail = $('#txt_email_alertEmail').val().trim();
            emailEntry.SenderEmail = $('#txt_email_senderEmail').val().trim();
            emailEntry.Id = $('#hid_email_id').val();
            emailEntry.AlertType = 2;
            return emailEntry;
        }

        function checkIsNoOpen() {
            var isSmsNoOpen = $('#rd_sms_isOpen_no').is(':checked'),
                isEmailNoOpen = $('#rd_email_isOpen_no').is(':checked'),
                $smsEles = $('#rd_sms_isOpen_no').closest('tr').siblings().find(':input'),
                $emailEles = $('#rd_email_isOpen_no').closest('tr').siblings().find(':input');
            if (isSmsNoOpen)
                $smsEles.attr('disabled', true);
            else
                $smsEles.removeAttr('disabled');
            if (isEmailNoOpen)
                $emailEles.attr('disabled', true);
            else
                $emailEles.removeAttr('disabled');
        }
    </script>    
}
